package com.petalsgroup.petals.web.servlet.user;

import com.petalsgroup.petals.pojo.AnimationSimpleVO;
import com.petalsgroup.petals.pojo.CommentsVO;
import com.petalsgroup.petals.pojo.ResultInfo;
import com.petalsgroup.petals.pojo.UserDO;
import com.petalsgroup.petals.service.user.CommentsService;
import com.petalsgroup.petals.service.user.MySelfService;
import com.petalsgroup.petals.service.user.UserService;
import com.petalsgroup.petals.service.user.impl.CommentsServiceImpl;
import com.petalsgroup.petals.service.user.impl.MySelfServiceImpl;
import com.petalsgroup.petals.service.user.impl.UserServiceImpl;
import com.petalsgroup.petals.web.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/show/*")
public class ShowMyselfServlet extends BaseServlet {
    private MySelfService mySelfService = new MySelfServiceImpl();
    private CommentsService commentsService = new CommentsServiceImpl();
    private UserService userService = new UserServiceImpl();

    //跳转他人主页
      public void toOther(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

          req.getRequestDispatcher("/WEB-INF/user_view/showMyself.jsp").forward(req,resp);
    }

    //获取他人信息
    public void getOtherMsg(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取username
        int uid;
        UserDO otherUser = null;
        String username = req.getParameter("username");
        if (username == null || "".equals(username)) {
            String parameter = req.getParameter("uid");
            if (parameter == null || "".equals(parameter)) {
                otherUser = (UserDO) req.getSession().getAttribute("otherUser");
                uid = otherUser.getUid();
            } else {
                uid = Integer.parseInt(parameter);
                otherUser = userService.findUserByUid(uid);
            }
        } else {
            otherUser = mySelfService.findUidByUsername(username);
            uid = otherUser.getUid();
        }
        //通过username去查找uid
        Integer attentionNum = mySelfService.findAttentionNum(uid);
        req.getSession().setAttribute("otherAttentionNum",attentionNum);
        //System.out.println(otherUser);
        req.getSession().setAttribute("otherUser",otherUser);

        resp.sendRedirect(req.getContextPath()+"/show/getOtherComments");

    }

    //获取他人评论
    public void getOtherComments(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserDO user = (UserDO) req.getSession().getAttribute("otherUser");
        //System.out.println(user);
        Integer uid = user.getUid();
        //通过uid查询评论信息 返回list
        List<CommentsVO> commentsList = commentsService.findCommentsByUid(uid);
        req.getSession().setAttribute("otherComments",commentsList);
        resp.sendRedirect(req.getContextPath()+"/show/getOtherCollect");
    }

    //获取他人收藏
    public void getOtherCollect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserDO user = (UserDO) req.getSession().getAttribute("otherUser");
        Integer uid = user.getUid();
        //通过uid查询收藏信息 返回list
        List<AnimationSimpleVO> collectList = mySelfService.findAidByUid(uid);
        req.getSession().setAttribute("otherCollect",collectList);
        resp.sendRedirect(req.getContextPath()+"/show/toOther");
    }

    //查询他人评论
    public void selectOtherComments(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String selectname = req.getParameter("selectname");
        if (selectname.equals("查询所有")){
            resp.sendRedirect(req.getContextPath()+"/show/getOtherComments");
        }else {
            List<CommentsVO> list = commentsService.findCommentsByAname(selectname);
            //System.out.println(list);
            req.getSession().setAttribute("otherComments",list);
            resp.sendRedirect(req.getContextPath()+"/show/toOther");
        }
    }

    //查询他人收藏
    public void selectOtherCollect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String selectCollectname = req.getParameter("selectCollectname");
        if (selectCollectname.equals("查询所有")){
            resp.sendRedirect(req.getContextPath()+"/show/getOtherCollect");
        }else {
            List<AnimationSimpleVO> list = mySelfService.findCollectByAname(selectCollectname);
            //System.out.println(list);
            req.getSession().setAttribute("otherCollect",list);
            resp.sendRedirect(req.getContextPath()+"/show/toOther");
        }
    }

    //关注他人
    public void attentionOther(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserDO user = (UserDO) req.getSession().getAttribute("user");
        Integer uid = user.getUid();

        UserDO otherUser = (UserDO) req.getSession().getAttribute("otherUser");
        Integer uided = otherUser.getUid();

        ResultInfo resultInfo = new ResultInfo();
        Boolean b = mySelfService.findAttention(uid,uided);
        if (b){
            //存在
            resultInfo.setFlag(true);
            resultInfo.setErrorMsg("已经关注过");
        }else {
            //不存在
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("可以关注");
            //添加到关注表
            mySelfService.saveAttention(uid,uided);
            //关注数+1

        }

        writeValue(resp,resultInfo);
    }
}
